import 'package:changxindianzishangwu/cache/local_cache.dart';
import 'package:changxindianzishangwu/http/base/ec_base_request.dart';
import 'package:changxindianzishangwu/http/request/create_title_request.dart';
import 'package:changxindianzishangwu/http/request/delete_title_request.dart';
import 'package:changxindianzishangwu/http/request/select_by_orderid.dart';
import 'package:changxindianzishangwu/http/request/select_title_request.dart';
import 'package:changxindianzishangwu/http/request/update_title_groupname_request.dart';
import 'package:changxindianzishangwu/http/request/update_title_request.dart';
import 'package:changxindianzishangwu/http/zd_net.dart';
import 'package:changxindianzishangwu/model/title_user_list_model.dart';
import 'package:changxindianzishangwu/model/titles_list.dart';
import 'package:changxindianzishangwu/util/toast.dart';

class TitleDao {
  static createTitle(String groupName, int userId) async {
    EcBaseRequest request = CreateTitleRequest();
    request
        .add("groupName", groupName)
        .add('userId', userId)
        .add("access_token", getAppLoginToken());

    var result = await ZdNet.getInstance().send(request);

    print("re==========${result}");
    if (result['resultCode'] == 1) {
      return result['data']['groupId'];
    } else {
      showToast(result['resultMsg']);
      return "error";
    }
  }

  static selectTitle() async {
    EcBaseRequest request = SelectTitileRequest();
    request.add("access_token", getAppLoginToken());
    var result = await ZdNet.getInstance().send(request);
    TitleList titleList = TitleList.fromJson(result);
    print("titile-================${titleList.toJson()}");
    return titleList;
  }

  static updateTitle(String groupId, List<String> userIdListStr) async {
    EcBaseRequest request = UpdateTitleRequest();
    request
        .add('groupId', groupId)
        .addList("userIdListStr", userIdListStr)
        .add("access_token", getAppLoginToken());

    var result = await ZdNet.getInstance().send(request);
    print("ceeeeeê==========${result}");
  }

  static deleteTitle(String groupId) async {
    EcBaseRequest request = DeleteTitleRequest();
    request.add("groupId", groupId).add("access_token", getAppLoginToken());
    var result = await ZdNet.getInstance().send(request);
    return result;
  }

  static selectByUserId(String groupId, String groupName) async {
    EcBaseRequest request = SelectByUserIdRequest();
    request
        .add("groupId", groupId)
        .add('groupName', groupName)
        .add("access_token", getAppLoginToken());
    var result = await ZdNet.getInstance().send(request);
    TitleUserListModel titleUserList = TitleUserListModel.fromJson(result);
    return titleUserList;
    print("--------!!!!!!!${result}");
  }

  static updateGroupName(String groupId, String groupName) async {
    EcBaseRequest request = UpdateTitleGroupNameRequest();
    request
        .add('groupId', groupId)
        .add('groupName', groupName)
        .add("access_token", getAppLoginToken());

    await ZdNet.getInstance().send(request);
  }
}
